package algotithm.acwing.unionfind.test836;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author zhouyanxiang
 * @Date 2021-01-2021/1/15-10:10
 */
public class Main {

    static class UnionFind {
        int[] p;
        public UnionFind (int n) {
            p = new int[n+1];
            for (int i = 1; i <= n; i++) {
                p[i] = i;
            }
        }

        public int find(int x) {
            if (x == p[x]) {
                return x;
            }
            return p[x] = find(p[x]);
        }

        public void union(int x,int y) {
            int rootX = find(x),rootY = find(y);
            if (rootX == rootY) {
                return;
            }
            p[rootX] = find(y);
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] num = reader.readLine().split(" ");
        int n = Integer.parseInt(num[0]);
        int m = Integer.parseInt(num[1]);

        UnionFind u = new UnionFind(n);
        while (m-- > 0) {
            num = reader.readLine().split(" ");
            int x = Integer.parseInt(num[1]);
            int y = Integer.parseInt(num[2]);
            if (num[0].equals("M")) {
                u.union(x,y);
            } else {
                if (u.find(x) == u.find(y)) {
                    System.out.println("Yes");
                } else {
                    System.out.println("No");
                }
            }
        }
        reader.close();
    }


}
